//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.come.until;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateTextUtil {
	public CreateTextUtil() {
	}

	public static byte[] getContent(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.isFile()) {
			return null;
		} else {
			long fileSize = file.length();
			if (fileSize > 2147483647L) {
				//system.out.println("file too big...");
				return null;
			} else {
				// 使用 try-with-resources 确保 FileInputStream 正确关闭
				try (FileInputStream fi = new FileInputStream(file)) {
					byte[] buffer = new byte[(int)fileSize];
					int offset = 0;

					int numRead;
					for(boolean var7 = false; offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0; offset += numRead) {
					}

					if (offset != buffer.length) {
						throw new IOException("Could not completely read file " + file.getName());
					} else {
						return buffer;
					}
				}
			}
		}
	}

	public static byte[] getContent2(String filePath) throws IOException {
		// 使用 try-with-resources 确保 FileInputStream 和 ByteArrayOutputStream 正确关闭
		try (FileInputStream in = new FileInputStream(filePath);
			 ByteArrayOutputStream out = new ByteArrayOutputStream(1024)) {
			byte[] temp = new byte[1024];

			int size;
			while((size = in.read(temp)) != -1) {
				out.write(temp, 0, size);
			}

			return out.toByteArray();
		}
	}

	public static void createFile(String path, byte[] content) throws IOException {
		// 使用 try-with-resources 确保 FileOutputStream 正确关闭
		try (FileOutputStream fos = new FileOutputStream(path)) {
			fos.write(content);
		}
	}
}

